package cn.appsys.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import cn.appsys.constants.Constants;
import cn.appsys.pojo.BackendUser;


//验证登录信息的拦截器
public class SysInterceptor extends HandlerInterceptorAdapter {

  //preHandle类型前置增强，在跳转到指定的页面/控制器之前，进行登录信息的验证。
  //验证通过就放行，否则跳转到登录页面
  @Override
  public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
      HttpSession session = request.getSession();
      BackendUser user = (BackendUser)session.getAttribute(Constants.USER_SESSION);
      if (user == null) {
          //跳转到后台的登录页面去
          response.sendRedirect(request.getContextPath() + "/bLogin/toLogin");
          return false;
      }
      return true;  //放行
  }
}
